package saxion.sensorframework.db.conn;

import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * \brief SQL Class used to connect to the database and to execute queries
 * 
 * This class is an singleton wich contains a connection to the database. The
 * properties of the connection are in the config.properties.
 * 
 * @author Erik & Martijn
 */
public final class SqlConnect {
	static Logger logger = Logger.getLogger(SqlConnect.class.getName());
	/**
	 * < log4j used for logging. <a
	 * href="http://logging.apache.org/log4j/1.2/">log4j website</a>
	 */

	static final String CONFIG_FILE = "config.properties";
	/** < Location of project properties file */

	public Connection conn;
	/** < The connection to the database */
	private PreparedStatement statement;
	/** < Prepared statement that will be used to execute on the database */
	public static SqlConnect db;
	/** < An instance of the class SqlConnect */

	private String url;
	/** < Url to the database */
	private String dbName;
	/** < Name of the database */
	private String driver;
	/** < Java jdbc driver */
	private String username;
	/** < Username wich is used on the database */
	private String password;

	/** < Password wich is used on the database */

	/**
	 * Constructor
	 * 
	 * @throws SQLException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	private SqlConnect() throws SQLException, InstantiationException,
			IllegalAccessException, ClassNotFoundException {

		readConfigFile();

		Class.forName(driver).newInstance();
		this.conn = (Connection) DriverManager.getConnection(url + dbName,
				username, password);
		logger.info("Connection made with DB:" + dbName);
	}

	/**
	 * Reads the config.properties wich contains the information for the
	 * database
	 */
	private void readConfigFile() {

		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(CONFIG_FILE));
		} catch (Exception e) {
			e.printStackTrace();
		}

		url = prop.getProperty("dbUrl");
		dbName = prop.getProperty("dbName");
		driver = prop.getProperty("dbDriver");
		username = prop.getProperty("dbUsername");
		password = prop.getProperty("dbPassword");
	}

	/**
	 * Returns a SqlConnect used the get the singleton
	 * 
	 * @return SqlConnect singleton
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static synchronized SqlConnect getDbCon()
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException, SQLException {
		if (db == null) {
			db = new SqlConnect();
		}
		return db;

	}

	/**
	 * Execute a query wich will be executed in the database
	 * 
	 * @param query
	 *            To be executed
	 * @return ResultSet with the results of the query
	 * @throws SQLException
	 */
	public ResultSet query(String query) throws SQLException {
		statement = db.conn.prepareStatement(query);
		ResultSet res = statement.executeQuery();
		return res;
	}

	/**
	 * Used to insert data into the database
	 * 
	 * @return Resultset Generated keys
	 */
	public ResultSet insert(String insertQuery) throws SQLException {
		statement = db.conn.prepareStatement(insertQuery,
				Statement.RETURN_GENERATED_KEYS);
		statement.executeUpdate();

		ResultSet result = statement.getGeneratedKeys();
		return result;

	}
}
